﻿using ProductCatalog.WebApi.DataLayer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using ProductCatalog.WebApi.Models;

namespace ProductCatalog.WebApi.Tests
{


    /// <summary>
    ///This is a test class for ProductRepositoryTest and is intended
    ///to contain all ProductRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ProductRepositoryTest
    {
        private Review[] _Reviews;

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestInitialize]
        public void Init()
        {
            _Reviews = new[]
            {
                new Review()
                {
                    reviewId = "5169bed10fdb8c0e7c671d32",
                    rating = 5
                },
                new Review()
                {
                    reviewId = "5170bed10fdb8c0e7c671d33",
                    rating = 4
                },
                new Review()
                {
                    reviewId = "5171bed10fdb8c0e7c671d34",
                    rating = 5
                },
            };
        }

        /// <summary>
        ///A test for GetProductReviews
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        public void GetProductReviewsTest()
        {
            ProductRepository target = new ProductRepository();
            string prodid = "5178f823bbb2732280d139cd";
            int skip = 1;
            int limit = 2;
            Reviews expected = new Reviews()
            {                
                reviews = _Reviews.Skip(skip).Take(limit),
                totalCount = 3
            };

            Reviews actual;
            actual = target.GetProductReviews(prodid, skip, limit);
            
            Assert.AreEqual(expected.totalCount, actual.totalCount);
            Assert.AreEqual(expected.reviews.Skip(0).First().reviewId, actual.reviews.Skip(0).First().reviewId);
            Assert.AreEqual(expected.reviews.Skip(1).First().reviewId, actual.reviews.Skip(1).First().reviewId);
        }
    }
}
